import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
// import HelloWorld from '@/components/HelloWorld'

Vue.use(Router)

const sysRouteConfig = [
  {
    path: '/',
    component: resolve => require(['@/views/login.vue'], resolve),
    meta: {noCheckSession: true}
  },
  {
    path: '/login',
    name: 'login',
    component: resolve => require(['@/views/login.vue'], resolve),
    meta: {noCheckSession: true}
  },
  {
    path: '/homepage',
    name: 'homepage',
    component: resolve => require(['@/views/homepage.vue'], resolve),
    meta: {noCheckSession: false},
    children: [
      {
        path: '/homepage',
        name: 'homepage',
        component: resolve => require(['@/views/home.vue'], resolve),
        meta: {noCheckSession: false}
      },
      {
        path: '/profile',
        name: 'profile',
        component: resolve => require(['@/views/profile.vue'], resolve),
        meta: {noCheckSession: false}
      },
    ]
  },
  {
    path: '/list',
    name: 'list',
    component: resolve => require(['@/views/list.vue'], resolve),
    meta: {noCheckSession: false}
  },
  {
    path: '/edit',
    name: 'edit',
    component: resolve => require(['@/views/edit.vue'], resolve),
    meta: {noCheckSession: false}
  },
  {
    path: '/report',
    name: 'report',
    component: resolve => require(['@/views/report.vue'], resolve),
    meta: {noCheckSession: false}
  },
  {
    path: '/scan',
    name: 'scan',
    component: resolve => require(['@/views/scan.vue'], resolve),
    meta: {noCheckSession: false}
  },
  {
    path: '/password',
    name: 'password',
    component: resolve => require(['@/views/password.vue'], resolve),
    meta: {noCheckSession: false}
  },
  {
    path: '/detail',
    name: 'detail',
    component: resolve => require(['@/views/detail.vue'], resolve),
    meta: {noCheckSession: false}
  },
]

const router = new Router({
  // mode: 'history',
  base: __dirname,
  routes: sysRouteConfig
})

router.beforeEach((to, from, next) => {
  if (store.getters.token) { // 已登录
    if (to.path === '/login') {
      next({path: '/'})
    } else {
      next()
    }
  } else {
    if (to.matched.some(record => !record.meta.noCheckSession)) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  }
})

export default router
